package com.shell.customer.group.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.customer.base.service.CusLvlService;
import com.shell.customer.base.service.CusRecService;
import com.shell.customer.group.service.CusGrpService;
import com.shell.customer.group.vo.Group;
import com.shell.customer.po.CusGrp;
import com.shell.customer.po.CusLvl;
import com.shell.customer.po.CusRec;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class GroupController {
	@Autowired
	private CusGrpService cusGrpService;
	@Autowired
	private CusRecService cusRecService;
	@Autowired
	private CusLvlService cusLvlService;

	@RequestMapping
	@ResponseBody
	@Logging(module = "������", methods = "����������")
	public List<Group> queryGroups(PageLimit limit, Group group) {
		return this.cusGrpService.queryGroups(limit, group);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "������", methods = "����������")
	public RetMsg validateGroupCode(String grpId, String grpCode) {
		CusGrp grp = this.cusGrpService.selectByCode(grpCode);
		if ((grp == null) || ((grpId != null) && (grpId.equals(grp.getGrpId())))) {
			return new RetMsg("success", false, "������������������");
		}
		return new RetMsg("error", false, "����������������");
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "������", methods = "����������")
	public RetMsg validateGroupName(String grpId, String grpName) {
		CusGrp grp = this.cusGrpService.selectByName(grpName);
		if ((grp == null) || ((grpId != null) && (grpId.equals(grp.getGrpId())))) {
			return new RetMsg("success", false, "������������������");
		}
		return new RetMsg("error", false, "����������������");
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "������", methods = "����������������������")
	public RetMsg validateGroupBuyerFinner(String grpId, String finId) {
		boolean ret = this.cusGrpService.validateGroupBuyerFinner(grpId, finId);
		if (!ret) {
			CusRec finner = (CusRec) this.cusRecService.selectByKey(finId);
			return new RetMsg("error", false, "��" + finner.getCusName() + "����������������������������");
		}
		return new RetMsg("success", false, "��������������");
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "������", methods = "������")
	public RetMsg createGroup(@RequestBody Group group) {
		String msg = this.cusGrpService.createGroup(group);
		if (msg != null) {
			return new RetMsg("error", false, msg);
		}
		return new RetMsg("success", true, "��������");
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "������", methods = "������")
	public RetMsg modifyGroup(@RequestBody Group group) {
		String msg = this.cusGrpService.modifyGroup(group);
		if (msg != null) {
			return new RetMsg("error", false, msg);
		}
		return new RetMsg("success", true, "��������");
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "������", methods = "������")
	public RetMsg deleteGroup(String grpIds) {
		if (StringUtil.emptyAndNull(grpIds)) {
			new RetMsg("error", false, "��������");
		}
		String[] grpIdArr = grpIds.split(",");
		this.cusGrpService.deleteGroup(grpIdArr);
		return new RetMsg("success", true, "��������");
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "������", methods = "����������������")
	public List<CusGrp> finCusGroupByTagCode(String cusId, String tagCodes) {
		return this.cusGrpService.selectCusGroupByTagCode(cusId, tagCodes);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "������", methods = "����������������")
	public List<CusRec> findGroupEnts(String grpId, String relType) {
		return this.cusGrpService.findGroupEnts(grpId, relType);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "������", methods = "����������������������")
	public List<CusLvl> getGrpCusLvl(String grpId) {
		return this.cusLvlService.getGrpCusLvl(grpId);
	}
}
